#!/usr/bin/env python
#coding:utf-8
import sys
import time
import getpass
import MySQLdb
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException 

reload(sys)
sys.setdefaultencoding('utf-8')     #编码

#连接数据库 
conn=MySQLdb.connect(
    host="localhost",
    user="root",
    passwd="toor",
    db="test",
    charset="utf8")

cursor = conn.cursor()    

#插入一条记录


browser = webdriver.Firefox()
browser.get('http://pachong.org/')
def spider():
tr = browser.find_elements_by_xpath('/html/body/div[2]/table/tbody/tr')
for i in range(0, len(tr)):
  #  td_id   = tr[i].find_element_by_xpath('td[1]').text.strip()
    td_ip   = tr[i].find_element_by_xpath('td[2]').text             #ip
    td_port = tr[i].find_element_by_xpath('td[3]').text             #port

    sql = "insert ignore into proxy (ip, port) values ('%s','%s')" % (td_ip,td_port)    #插入数据库
    try:                                                                            #try一下是若数据库出错，把错误信息打印出来
        n = cursor.execute(sql)
    except Exception,e:
        print e,sql
    while true:
        spider()
        time.sleep(3600)



'''
proxy_list = browser.find_elements_by_xpath('/html/body/div[2]/table/tbody/tr/td[2]')
print len(proxy_list)
for i in range(0, len(proxy_list)):
	print proxy_list[i].text

port_list = browser.find_elements_by_xpath('/html/body/div[2]/table/tbody/tr/td[3]')
print len(port_list)
for i in range(0, len(port_list)):
	print port_list[i].text
'''